//
//  ViewController.m
//  changeView
//
//  Created by Marcelo Perretta on 6/19/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"
#import "CompanyViewController.h"
#import "LogoViewController.h"
#import "ApplicationViewController.h"

@implementation ViewController

@synthesize company = _company;
@synthesize logo = _logo;
@synthesize appManager = _appManager;

//@synthesize activityIndicatorView = _activityIndicatorView;


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    CompanyViewController *companyView = [[CompanyViewController alloc] initWithNibName:@"CompanyViewController" bundle:nil];
	self.company = companyView;
	[self.view insertSubview:companyView.view atIndex:0];
	[companyView release];
    
    
    /*
     Despues de 2 segundos de mostrar el logo de la empresa, pasar al logo de la compania y chequear las conexiones:
     
     - Comprobar que tenga conexion a Internet
     - Comprobar que tenga conexion a la posicion
     - Comprobar si esta registrado
     */
    [NSTimer scheduledTimerWithTimeInterval:1 
                                     target:self 
                                   selector:@selector(showLogoApp:) 
                                   userInfo:nil
                                    repeats:NO];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

-(void) dealloc{
    [_company release];
    [_logo release];
    [_appManager release];
    [super release];
}

#pragma mark - my Methods

- (void) showLogoApp:(NSTimer *) timer {
    LogoViewController *logoView = [[LogoViewController alloc] initWithNibName:@"LogoViewController" bundle:nil];
    self.logo = logoView;
    
    [logoView release];
    
    [_company.view removeFromSuperview];
    [self.view insertSubview:logoView.view atIndex:0];
    
    [NSTimer scheduledTimerWithTimeInterval:2 
                                     target:self 
                                   selector:@selector(muchoAppStarted:) 
                                   userInfo:nil
                                    repeats:NO];
    
    // Setting Up Activity Indicator View
    /*self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.activityIndicatorView.hidesWhenStopped = YES;
    self.activityIndicatorView.center = self.view.center;
    [self.view addSubview:self.activityIndicatorView];
    [self.activityIndicatorView startAnimating];
    
    if([self checkInternetConections]){
        NSLog(@"Chequear gpse");
    } else {
        NSLog(@"Generar Alert");
    }*/
    
}

- (void) muchoAppStarted:(NSTimer *) timer {

    ApplicationViewController *applicationView = [[ApplicationViewController alloc] initWithNibName:@"ApplicationViewController" bundle:nil];
    self.appManager = applicationView;
    
    [applicationView release];
    
    [_logo.view removeFromSuperview];
    [self.view insertSubview:applicationView.view atIndex:0];

}


@end
